﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.IBLL.Dictionary;
using Base.Core.UnityFactory;
using BF.Model;
using BF.Infrastructure;

namespace BF.Web.pages.BaseSystem.UserManagement
{
    public partial class ChooseLowerOrganization : BasePage
    {
        #region 实例
        Idic_OrganizationBLL iBLL = IOCFactory.GetIOCResolve<Idic_OrganizationBLL>();
        List<dic_Dict> dicOrganizationTypeList = new List<dic_Dict>();
        List<dic_Dict> dicOrganizationLevelList = new List<dic_Dict>();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControl();
                BindList();
            }
        }

        protected string GetDivisionName(string sCode)
        {
            return GetAreaNameByAreaCode(sCode);
        }
        /// <summary>
        /// 获取机构级别名称
        /// </summary>
        /// <param name="sCode"></param>
        /// <returns></returns>
        protected string GetsOrganizationLevelName(string sCode)
        {
            if (dicOrganizationLevelList == null || dicOrganizationLevelList.Count == 0)
                dicOrganizationLevelList = GetDictList("sOrganizationLevel");
            var tempModel = dicOrganizationLevelList.Where(P => P.sCode == sCode).FirstOrDefault();
            if (tempModel != null)
            {
                return dicOrganizationLevelList.Where(P => P.sCode == sCode).FirstOrDefault().sName;
            }
            return "";

        }
        /// <summary>
        /// 获取机构类型名称
        /// </summary>
        /// <param name="sCode"></param>
        /// <returns></returns>
        protected string GetOrganizationTypeName(string sCode)
        {
            if (dicOrganizationTypeList == null || dicOrganizationTypeList.Count == 0)
                dicOrganizationTypeList = GetDictList("sOrganizationType");
            var tempModel = dicOrganizationTypeList.Where(P => P.sCode == sCode).FirstOrDefault();
            if (tempModel != null)
            {
                return dicOrganizationTypeList.Where(P => P.sCode == sCode).FirstOrDefault().sName;
            }
            return "无";
        }
        private void BindControl()
        {
            System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem
            {
                Value = "",
                Text = "--请选择--"
            };

            //绑定机构类型
            if (sOrganizationType == null || dicOrganizationTypeList.Count == 0)
                dicOrganizationTypeList = GetDictList("sOrganizationType");
            this.sOrganizationType.DataSource = dicOrganizationTypeList;
            this.sOrganizationType.DataValueField = "sCode";
            this.sOrganizationType.DataTextField = "sName";
            this.sOrganizationType.DataBind();
            this.sOrganizationType.Items.Insert(0, li);
            //绑定机构级别
            if (dicOrganizationLevelList == null || dicOrganizationLevelList.Count == 0)
                dicOrganizationLevelList = GetDictList("sOrganizationLevel").OrderBy(P => P.sCode).ToList();
            this.sOrganizationLevel.DataSource = dicOrganizationLevelList;
            this.sOrganizationLevel.DataValueField = "sCode";
            this.sOrganizationLevel.DataTextField = "sName";
            this.sOrganizationLevel.DataBind();
            this.sOrganizationLevel.Items.Insert(0, li);
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }

        private void BindList()
        {
            string sAreaCode = "340100";
            dic_Organization dicModel = GetSessionCurrentUserInfo.dic_Organization;
            int count = 0;//定义数据行
            List<dic_Organization> list = new List<dic_Organization>();//定义数据集合
            if (dicModel != null)
            {
                //登陆人员的机构信息不为空时
                if (dicModel.gHigherKey == default(Guid))
                {//如果登录人员的上级机构key为default(Guid)时查询所有上级机构Key不等于 default(Guid)的机构信息
                    count = iBLL.GetRowCount(
                        P =>
                        (String.IsNullOrEmpty(sOrganizationName.Text.Trim()) ? true : P.sOrganizationName.Contains(sOrganizationName.Text.Trim()))
                        && (String.IsNullOrEmpty(sOrganizationLevel.SelectedValue) ? true : (P.sOrganizationLevel == sOrganizationLevel.SelectedValue.Trim()))
                        && (String.IsNullOrEmpty(sOrganizationType.SelectedValue) ? true : (P.sOrganizationType == sOrganizationType.SelectedValue.Trim()))
                        && (P.iUse == 1)
                        && (P.sAreaCode == sAreaCode)
                        );

                    list = iBLL.GetList(
                        P =>
                        (String.IsNullOrEmpty(sOrganizationName.Text.Trim()) ? true : P.sOrganizationName.Contains(sOrganizationName.Text.Trim()))
                        && (String.IsNullOrEmpty(sOrganizationLevel.SelectedValue) ? true : (P.sOrganizationLevel == sOrganizationLevel.SelectedValue.Trim()))
                        && (String.IsNullOrEmpty(sOrganizationType.SelectedValue) ? true : (P.sOrganizationType == sOrganizationType.SelectedValue.Trim()))
                        && (P.iUse == 1)
                        && (P.sAreaCode == sAreaCode)
                        ,
                        P => new { P.sOrganizationType, P.sOrganizationCode },
                        new PageModel(AspNetPager1.CurrentPageIndex), true);
                }
                else
                {
                    count = iBLL.GetRowCount(
                        P =>
                        (String.IsNullOrEmpty(sOrganizationName.Text.Trim()) ? true : P.sOrganizationName.Contains(sOrganizationName.Text.Trim()))
                        && (String.IsNullOrEmpty(sOrganizationLevel.SelectedValue) ? true : (P.sOrganizationLevel == sOrganizationLevel.SelectedValue.Trim()))
                        && (String.IsNullOrEmpty(sOrganizationType.SelectedValue) ? true : (P.sOrganizationType == sOrganizationType.SelectedValue.Trim()))
                        && (P.iUse == 1)
                        && (P.sAreaCode == sAreaCode)
                        && (P.gHigherKey == dicModel.gKey || P.gKey == dicModel.gKey)
                        );

                    list = iBLL.GetList(
                        P =>
                        (String.IsNullOrEmpty(sOrganizationName.Text.Trim()) ? true : P.sOrganizationName.Contains(sOrganizationName.Text.Trim()))
                        && (String.IsNullOrEmpty(sOrganizationLevel.SelectedValue) ? true : (P.sOrganizationLevel == sOrganizationLevel.SelectedValue.Trim()))
                        && (String.IsNullOrEmpty(sOrganizationType.SelectedValue) ? true : (P.sOrganizationType == sOrganizationType.SelectedValue.Trim()))
                        && (P.iUse == 1)
                        && (P.sAreaCode == sAreaCode)
                        && (P.gHigherKey == dicModel.gKey || P.gKey == dicModel.gKey)
                        ,
                        P => new { P.sOrganizationType, P.sOrganizationCode },
                        new PageModel(AspNetPager1.CurrentPageIndex), true);
                }
            }
            else
            {
                count = iBLL.GetRowCount(
                    P =>
                    (String.IsNullOrEmpty(sOrganizationName.Text.Trim()) ? true : P.sOrganizationName.Contains(sOrganizationName.Text.Trim()))
                    && (String.IsNullOrEmpty(sOrganizationLevel.SelectedValue) ? true : (P.sOrganizationLevel == sOrganizationLevel.SelectedValue.Trim()))
                    && (String.IsNullOrEmpty(sOrganizationType.SelectedValue) ? true : (P.sOrganizationType == sOrganizationType.SelectedValue.Trim()))
                    && (P.iUse == 1)
                    && (P.sAreaCode == sAreaCode)
                    );

                list = iBLL.GetList(
                    P =>
                    (String.IsNullOrEmpty(sOrganizationName.Text.Trim()) ? true : P.sOrganizationName.Contains(sOrganizationName.Text.Trim()))
                    && (String.IsNullOrEmpty(sOrganizationLevel.SelectedValue) ? true : (P.sOrganizationLevel == sOrganizationLevel.SelectedValue.Trim()))
                    && (String.IsNullOrEmpty(sOrganizationType.SelectedValue) ? true : (P.sOrganizationType == sOrganizationType.SelectedValue.Trim()))
                    && (P.iUse == 1)
                    && (P.sAreaCode == sAreaCode)
                    ,
                    P => new { P.sOrganizationType, P.sOrganizationCode },
                    new PageModel(AspNetPager1.CurrentPageIndex), true);
            }
            AspNetPager1.RecordCount = count;
            Repeater1.DataSource = list;
            Repeater1.DataBind();
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        { AspNetPager1.CurrentPageIndex = 1; }
    }
}